// pages/company/order/order.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navIndex:1,
    items:[],
    pageIndex:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    var that=this
    wx.showLoading({
      title: "加载中"
    });
  var e={
    currentTarget:{
      dataset:{
        index: that.data.navIndex
      }
    }
  }
    that.bottom()

    
    that.setData({
      navIndex: Number(e.currentTarget.dataset.index)
    })      
  },
  search(){//搜索

  },
  nav(e){
    var that =this
    that.setData({
      items:[]
    });    
    var company = wx.getStorageSync("COMPANY");
    that.setData({
      navIndex: Number(e.currentTarget.dataset.index)
    })
    that.bottom()
  },
  bottom(){
    console.log("11111111111111")
    var that =this
    var company = wx.getStorageSync("COMPANY");
    app.getApisj.request(
      "/api/Orders/List", {
        company: {
          id: company.id,
          companyName: company.companyName,
        },
        orderStatus: that.data.navIndex,
        PageIndex: that.data.pageIndex,
        pageSize: 10

      },
      "POST",
      res => {
        wx.hideLoading();
        if (res.data.responseCode == 1000) {
          //列表数据
          for (var i = 0; i < res.data.obj.length; i++) {
            res.data.obj[i].createTime = that.changeTime(res.data.obj[i].createTime)
          }
          that.setData({
            items: that.data.items.concat(res.data.obj),
            onSh: true
          });
        } else {
          wx.showToast({
            title: '请求失败',
            icon: 'loading',
            duration: 2000
          });
        }

      },
      res => { }
    );
  },
  todetails(e){//跳转订单明细
  var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../details/details?id='+id,
    })
  },
  todetailsy(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../detailsy/detailsy?id=' + id,
    })   
  },
  changeTime(timestamp) {
    timestamp = timestamp.replace(/-/g, '/');
    var time = Date.parse(new Date(timestamp)); 
    //时间转换
    var minute = 1000 * 60
    var hour = minute * 60
    var day = hour * 24
    var month = day * 30
    var diffValue = new Date().getTime() - time;
    if (diffValue < minute) //一分钟内							
      return "一分钟"
    else if (diffValue < hour)
      return parseInt(diffValue / minute) + "分钟"
    else if (diffValue < day)
      return parseInt(diffValue / hour) + "小时"
    else if (diffValue < month)
      return parseInt(diffValue / day) + "天"
  },

  onReachBottom() {
    var that = this;
      that.setData({
        pageIndex: that.data.pageIndex + 1
      });
      var e = {
        currentTarget: {
          dataset: {
            index: that.data.navIndex
          }
        }
      }  
      that.bottom()
  },

  // 添加寄件 
  createSendGoods () {
    wx.navigateTo({
      url: '../sendGoods/sendGoods',
    })
  }
})